<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\service;

use app\wechat\common\WechatException;

final class Blacklist
{
//     public function download_blacklist($mp_number)
//     {
//         $now_time = time();

//         helper('admin/sql')->execute_sql('TRUNCATE {wechat_blacklist_sync}');

//         $downloaded = $this->get_blacklist($mp_number);
//         foreach ($downloaded['data'] as $openid) {
//             $sql = "INSERT INTO {wechat_blacklist}
//                     (
//                         mp_number,
//                         openid,
//                         create_time,
//                         update_time,
//                         sync_time,
//                         sync_msg
//                     )
//                     VALUES
//                     (
//                         :mp_number,
//                         :openid,
//                         :now_time,
//                         :now_time,
//                         :now_time,
//                         ''
//                     )";
//             helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'openid' => $openid, 'now_time' => $now_time));
//         }

//         $sql = "UPDATE {wechat_blacklist}
//                    SET sync_date = :now_time,
//                        sync_msg = ''
//                   FROM {wechat_blacklist} w,
//                        {wechat_blacklist_sync} t
//                  WHERE w.mp_number = t.mp_number
//                    AND w.openid = t.openid";
//         helper('admin/sql')->execute_sql($sql, array('now_time' => $now_time));

//         $sql = "INSERT INTO {wechat_blacklist}
//                 (
//                        mp_number,
//                        openid,
//                        create_time,
//                        update_time,
//                        sync_time,
//                        sync_msg
//                 )
//                 SELECT mp_number,
//                        openid,
//                        :now_time,
//                        :now_time,
//                        :now_time,
//                        ''
//                   FROM {wechat_blacklist_sync} t
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist} WHERE mp_number = t.mp_number AND openid = t.openid)
//                    AND mp_number = :mp_number";
//         helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'now_time' => $now_time));

//         $sql = "SELECT openid
//                   FROM {wechat_blacklist} w
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist_sync} WHERE mp_number = w.mp_number AND openid = w.openid)
//                    AND mp_number = :mp_number";
//         $list = helper('admin/sql')->query_sql($sql, array('mp_number' => $mp_number));
//         if ($list) {
//             foreach ($list as $openid) {
//                 $this->add_openid_to_blacklist($mp_number, $openid);

//                 $sql = "UPDATE {wechat_blacklist}
//                     SET sync_date = :now_time,
//                         sync_msg = ''
//                    FROM {wechat_blacklist} w,
//                         {wechat_blacklist_sync} t
//                   WHERE w.mp_number = t.mp_number
//                     AND w.openid = t.openid";
//                 helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'openid' => $openid));
//             }
//         }

//         $sql = "SELECT openid
//                   FROM {wechat_blacklist_sync} t
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist} WHERE mp_number = t.mp_number AND openid = t.openid)
//                    AND mp_number = :mp_number";
//         $list = helper('admin/sql')->query_sql($sql, array('mp_number' => $mp_number));
//         if ($list) {
//             foreach ($list as $openid) {
//                 $this->remove_openid_to_blacklist($mp_number, $openid);
//             }
//         }
//     }

//     public function sync_blacklist($mp_number)
//     {
//         $now_time = time();

//         helper('admin/sql')->execute_sql('TRUNCATE {wechat_blacklist_sync}');

//         $downloaded = $this->download_blacklist($mp_number);
//         foreach ($downloaded['data'] as $openid) {
//             $insert_data['mp_number'] = $mp_number;
//             $insert_data['openid'] = $openid;

//             $sql = "INSERT INTO {wechat_blacklist}
//                     (
//                         mp_number,
//                         openid,
//                         create_time,
//                         update_time,
//                         sync_time,
//                         sync_msg
//                     )
//                     VALUES
//                     (
//                         :mp_number,
//                         :openid,
//                         :now_time,
//                         :now_time,
//                         :now_time,
//                         ''
//                     )";
//             helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'openid' => $openid, 'now_time' => $now_time));
//         }

//         $sql = "UPDATE {wechat_blacklist}
//                    SET sync_date = :now_time,
//                        sync_msg = ''
//                   FROM {wechat_blacklist} w,
//                        {wechat_blacklist_sync} t
//                  WHERE w.mp_number = t.mp_number
//                    AND w.openid = t.openid";
//         helper('admin/sql')->execute_sql($sql, array('now_time' => $now_time));

//         $sql = "INSERT INTO {wechat_blacklist}
//                 (
//                        mp_number,
//                        openid,
//                        create_time,
//                        update_time,
//                        sync_time,
//                        sync_msg
//                 )
//                 SELECT mp_number,
//                        openid,
//                        :now_time,
//                        :now_time,
//                        :now_time,
//                        ''
//                   FROM {wechat_blacklist_sync} t
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist} WHERE mp_number = t.mp_number AND openid = t.openid)
//                    AND mp_number = :mp_number";
//         helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'now_time' => $now_time));

//         $sql = "SELECT openid
//                   FROM {wechat_blacklist} w
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist_sync} WHERE mp_number = w.mp_number AND openid = w.openid)
//                    AND mp_number = :mp_number";
//         $list = helper('admin/sql')->query_sql($sql, array('mp_number' => $mp_number));
//         if ($list) {
//             foreach ($list as $openid) {
//                 $this->add_blacklist($mp_number, $openid);

//                 $sql = "UPDATE {wechat_blacklist}
//                     SET sync_date = :now_time,
//                         sync_msg = ''
//                    FROM {wechat_blacklist} w,
//                         {wechat_blacklist_sync} t
//                   WHERE w.mp_number = t.mp_number
//                     AND w.openid = t.openid";
//                 helper('admin/sql')->execute_sql($sql, array('mp_number' => $mp_number, 'openid' => $openid));
//             }
//         }

//         $sql = "SELECT openid
//                   FROM {wechat_blacklist_sync} t
//                  WHERE NOT EXISTS (SELECT 1 FROM {wechat_blacklist} WHERE mp_number = t.mp_number AND openid = t.openid)
//                    AND mp_number = :mp_number";
//         $list = helper('admin/sql')->query_sql($sql, array('mp_number' => $mp_number));
//         if ($list) {
//             foreach ($list as $openid) {
//                 $this->remove_blacklist($mp_number, $openid);
//             }
//         }
//     }

//     public function get_blacklist($mp_number)
//     {
//         $wxobj = service('wechat/mp')->get_wx_object($mp_number);

//         $array['total'] = 0;
//         $array['data'] = array();
//         $begin_openid = '';

//         while (true) {
//             $result = $wxobj->get_blacklist(array('begin_openid' => $begin_openid));
//             if ($result === false) {
//                 throw new WechatException($wxobj);
//             }

//             $array['total'] = $result['total'];
//             if (isset($result['data'])) {
//                 $array['data'] = array_merge($array['data'], $result['data']['openid']);
//             }

//             if (isset($result['next_openid'])) {
//                 $begin_openid = $result['next_openid'];
//             } else {
//                 break;
//             }
//         }

//         return $array;
//     }

    public function add_blacklist($mp_number, $openid)
    {
        $wxobj = service('wechat/mp')->get_wx_object($mp_number);

        if (is_array($openid)) {
            $openid_list = array();
            $last_list = array();

            foreach ($openid as $id) {
                if (count($last_list) < 20) {
                    $last_list[] = $id;
                } else {
                    $openid_list[] = $last_list;
                    $last_list = array($id);
                }
            }

            if ($last_list) {
                $openid_list[] = $last_list;
            }
        } else {
            $openid_list[] = array($openid);
        }

        foreach ($openid_list as $list) {
            $result = $wxobj->add_blacklist(array('openid_list' => $list));
            if ($result === false) {
                throw new WechatException($wxobj);
            }
        }

        $where_map['mp_number'] = array('eq', $mp_number);
        $where_map['openid'] = array('in', (array)$openid);
        model('wechat/user')->where($where_map)->update(array('blacklist' => 1));
    }

    public function remove_blacklist($mp_number, $openid)
    {
        $wxobj = service('wechat/mp')->get_wx_object($mp_number);

        if (is_array($openid)) {
            $openid_list = array();
            $last_list = array();

            foreach ($openid as $id) {
                if (count($last_list) < 20) {
                    $last_list[] = $id;
                } else {
                    $openid_list[] = $last_list;
                    $last_list = array($id);
                }
            }

            if ($last_list) {
                $openid_list[] = $last_list;
            }
        } else {
            $openid_list[] = array($openid);
        }

        foreach ($openid_list as $list) {
            $result = $wxobj->remove_blacklist(array('openid_list' => $list));
            if ($result === false) {
                throw new WechatException($wxobj);
            }
        }

        $where_map['mp_number'] = array('eq', $mp_number);
        $where_map['openid'] = array('in', (array)$openid);
        model('wechat/user')->where($where_map)->update(array('blacklist' => 0));
    }
}
